/*
 * LooperAdapter.java
 * business
 *
 * Created by ChenTao on 2017/3/14.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.banner;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.yidiandao.business.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.banner.BannerAdapter;
import in.srain.cube.views.banner.SliderBanner;

/**
 * AlwaysLooperAdapter
 * <p>
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/3/14 0014 15:37
 */
public class LooperAdapter extends BannerAdapter {

    private Context mContext;

    private SliderBanner mBannerView;

    private List<ImageView> mImageViews = new ArrayList<>();

    private List<String> mImageUrls = new ArrayList<>();

    private List<String> mSourceUrls = new ArrayList<>();

    private OnBannerItemClickListener mListener;

    public LooperAdapter(Context context) {
        mContext = context;
    }

    public void injectBannerView(SliderBanner bannerView) {
        mBannerView = bannerView;
        mBannerView.setAdapter(this);
    }

    public void setImageUrls(List<String> imageUrls) {

        // reset source urls
        mSourceUrls.clear();
        mSourceUrls.addAll(imageUrls);

        // reset imageUrls for show
        mImageUrls.clear();
        mImageUrls.addAll(imageUrls);

        // clear All ImageViews
        mImageViews.clear();

        // init ImageViews
        addImageViews(imageUrls);

        // fix 2 or 3 pagers bug
        if (mImageViews.size() > 1 && mImageViews.size() < 4) {
            addImageViews(imageUrls);
            mImageUrls.addAll(imageUrls);
        }

        notifyDataSetChanged();

        // stat play Banner
        play();
    }

    private void play() {

        if (mBannerView == null) {
            throw new NullPointerException(
                    "You must call method `injectBannerView(SliderBanner)` first !");
        }

        if (mSourceUrls.size() != 1) {
            mBannerView.setDotNum(getDotNum());
            mBannerView.beginPlay();
        } else {
            mBannerView.setDotNum(0);
            mBannerView.stopPlay();
        }
    }

    private void addImageViews(List<String> imageUrls) {
        for (int i = 0; i < imageUrls.size(); i++) {
            ImageView imageView = new ImageView(mContext);
            mImageViews.add(imageView);
        }
    }

    @Override
    public View getView(LayoutInflater layoutInflater, final int position) {
        ImageView imageView = mImageViews.get(position % mImageViews.size());
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ImageLoader.loadBanner(mContext, imageView, mImageUrls.get(position % mImageViews.size()));
        // trigger
        if (mListener != null) {
            imageView.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // calculate real position of source Data
                            int pos = position % mImageViews.size();
                            if (pos >= mSourceUrls.size()) {
                                pos = pos - mSourceUrls.size();
                            }
                            mListener.onBannerItemClick(pos);
                        }
                    });
        }

        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ImageView imageView = mImageViews.get(position % mImageViews.size());
        Glide.clear(imageView);
        container.removeView(imageView);
    }

    @Override
    public int getPositionForIndicator(int position) {
        int pos = position % mImageUrls.size();
        if (pos >= mSourceUrls.size()) {
            pos = pos - mSourceUrls.size();
        }
        return pos;
    }

    private int getDotNum() {
        return mSourceUrls.size();
    }

    @Override
    public int getCount() {
        if (mSourceUrls.size() == 0 || mSourceUrls.size() == 1) {
            return mSourceUrls.size();
        }
        return Integer.MAX_VALUE;
    }

    public void setOnBannerItemClickListener(OnBannerItemClickListener listener) {
        mListener = listener;
    }

    public interface OnBannerItemClickListener {
        void onBannerItemClick(int position);
    }
}
